package com.ms.education.statistics.schedule;

import cn.dev33.satoken.same.SaSameUtil;
import com.ms.education.constant.RedissonConstant;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * <p>Project:education-SaTokenSchedule
 * <p>Powered by ms on 2023-03-12 23:42:52
 *
 * @author ms
 * @version 1.0
 * @since 1.8
 */
@Component
@Slf4j
public class SaTokenSchedule {

    @Autowired
    private RedissonClient redissonClient;

    // 从 0 分钟开始 每隔 5 分钟执行一次 Same-Token
    @Scheduled(cron = "0 0/1 * * * ? ")
    public void refreshToken() {
        RLock lock = redissonClient.getLock(RedissonConstant.REFRESH_SAME_TOKEN);
        if (lock.tryLock()) {
            try {
                String refreshToken = SaSameUtil.refreshToken();
                log.info("刷新了same-token，新的值为===={}", refreshToken);
            } finally {
                lock.unlock();
            }
        }
    }

}
